package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BCrypt;
import com.usian.common.util.BeanHelper;
import com.usian.common.util.JwtUtils;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.dtos.WmUserLoginDto;
import com.usian.wemedia.entity.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-05
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {
    /**
     * 根据这个id查询是否存在
     * @param ApUserId
     * @return
     */
    @Override
    public WmUserDto findByApUserId(Integer ApUserId) {
        //根据id查询是否存在
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getApUserId,ApUserId);
        WmUser wmUser = getOne(lambdaQueryWrapper);
        //存在的话就返回自媒体用户的dto端信息
        if (!StringUtils.isEmpty(wmUser)){
            return BeanHelper.copyProperties(wmUser,WmUserDto.class);
        }
        //不存在的话就返回一个空
        return null;
    }

    @Override
    public Integer saveWmUser(WmUserDto wmUserDto) {
        if (StringUtils.isEmpty(wmUserDto)){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"参数不正确");
        }
        //对wmUserdto进行校验 如果数据库中存在
        WmUserDto wmUserDto1 = findByApUserId(wmUserDto.getApUserId());
        //对wmUserDto1校验
        if (!StringUtils.isEmpty(wmUserDto1)){
           //不为空的话就返回id
            return wmUserDto1.getId();
        }
        //如果为空创建自媒体用户 对数据库进行交互时需要转换
        WmUser wmUser = BeanHelper.copyProperties(wmUserDto, WmUser.class);
        wmUser.setCreatedTime(new Date());
        wmUser.setType(9);
        wmUser.setType(0);
        boolean b = save(wmUser);
        //如果添加失败返回
        if (!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"创建自媒体用户失败");
        }
        //如果返回这个id
        return wmUser.getId();
    }
    /**
     * 校验账户密码
     * @param wmUserLoginDto
     * @return
     */
    @Autowired
    private BCryptPasswordEncoder encoder;

    @Override
    public Map<String, Object> login(WmUserLoginDto wmUserLoginDto) {
        //如果为空的话抛出异常
        if (StringUtils.isEmpty(wmUserLoginDto)){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"参数异常");
        }
        //如果不为空的话对账户进行校验 查看账户是否存在
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getName,wmUserLoginDto.getName());
        WmUser wmUser = getOne(queryWrapper);
        //如果查询的用户为空 返回该用户不存在
        if (StringUtils.isEmpty(wmUser)){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在！");
        }
        //如果用户不为空 对密码进行校验
        boolean b = encoder.matches(wmUserLoginDto.getPassword(), wmUser.getPassword());
        //如果不等于true
        if (!b){
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //密码对的话进行封装 生成token
        String token = JwtUtils.generateTokenExpireInSeconds(wmUser.getId(), 60*24*7);
        HashMap<String,Object> map = new HashMap();
        wmUser.setPassword("");
        map.put("user",wmUser);
        map.put("token",token);

        return map;
    }
}
